package com.my.study.controller;

import cn.hutool.core.io.FileUtil;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author: yidujun
 * @create: 2021/09/15 20:27
 */
@RequestMapping("/amz/s3")
@RestController
public class FileUploadController {

    @Value("${s3.path}")
    private String path;

    @Value("${amazon.s3.bucket-name}")
    private String bucketName;

    @Resource
    AmazonS3 amazonS3;

    @RequestMapping("/upload")
    public void upload() {
        String fileName = "raft-2014.txt";
        String filePath = path + fileName;
        File file = FileUtil.file(filePath);
        PutObjectRequest request = new PutObjectRequest(bucketName, fileName, file);

        try {
            amazonS3.putObject(request);
        } catch (SdkClientException e) {
            e.printStackTrace();
        }

    }

}
